<template>
  <!-- 一条文章单元格 -->
  <div>
    <!-- get-container 设置挂载点 -->
    <van-action-sheet v-model="show" :actions="actions" @select="onSelect" get-container="body" :cancel-text="cancelText" @cancel="cancelFn" @close="closeFn"/>
    <van-cell>
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{obj.title}}</span>
        <!-- 单张图 -->
        <img class="thumb" v-if="obj.cover.type === 1 " img :src="obj.cover.images[0]" />
      </div>
      <!-- 三张图 -->
      <div class="thumb-box" v-if="obj.cover.type===3" >
        <img class="thumb" v-for="imgURL,index in obj.cover.images"  :key="index" :src="imgURL" alt="">
      </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>{{obj.aut_name}}</span>
          <span>{{obj.comm_count}}评论</span>
          <span>{{getTime(`${obj.pubdate}`)}}</span>
        </div>
        <!-- 反馈按钮 -->
        <van-icon name="cross" @click.stop="closeross" v-if="isShow"/>
      </div>
    </template>
  </van-cell>
  </div>
</template>

<script>
import { getTime } from '@/utils/date'
import { firstActions, secondActions } from '@/api/report'
import { getDislikeAPI, getArtReportAPI } from '@/api'
export default {
  name: 'ArticleItem',
  props: {
    obj: Object, // 父传子传递过来的文章列表对象
    isShow: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      show: false,
      actions: firstActions,
      cancelText: '取消'
    }
  },
  methods: {
    // 处理时间
    getTime: getTime,
    // 反馈
    async onSelect (item) {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      // 如果点击反馈垃圾内容 进入下一阶段否则 关闭弹框
      if (item.name === '反馈垃圾内容') {
        this.actions = secondActions
        this.cancelText = '返回'
      } else if (item.name === '不感兴趣') {
        // 如果点击不感兴趣 调用接口
        await getDislikeAPI(this.obj.art_id)
        // console.log(res)
        this.$Notify({ type: 'success', message: '反馈成功' })
        this.show = false
      } else {
        // console.log(item)
        // 点击其他情况（二级面板==>举报）
        const avgObj = {
          art_id: this.obj.art_id,
          type: item.value
        }
        if (item.value === 0) {
          avgObj.remark = '其他问题'
        }
        await getArtReportAPI(avgObj)
        this.$Notify({ type: 'success', message: '举报成功' })
        this.show = false
      }
    },
    closeross () {
      this.show = true
    },
    cancelFn () {
      this.show = true // 没有办法阻止弹框关闭就设置打开它
      if (this.cancelText === '返回') {
        this.actions = firstActions
        this.cancelText = '取消'
      } else {
        this.show = false
      }
    },
    // 不管在哪层关闭弹框 都将恢复到最初状态 ==>打开是在第一层
    closeFn () {
      this.actions = firstActions
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
